__author__ = 'wangd'
# coding: utf-8
# Time : 2017/2/19 16:37
from flask import Flask, request,json
import re,time
from ManualData import parse_client_log
from PublicMethod import hz_encode,urldata
# md_log=time.strftime('%Y-%m-%d-%H-%M',time.localtime(time.time()))+".txt"
md_log="test.txt"
zhPattern = re.compile(u'[\u4e00-\u9fa5]+')
app = Flask(__name__)
@app.route('/index.php',methods = ['POST','GET'])
def coverAdversApi():
    dataurl=request.url
    urlencode=hz_encode(dataurl)
    action=request.args.get("action")
    if action=="parse_client_log":
        if request.method == 'POST':
            a=request.form.to_dict()
            b=str(a).split("\\n")
            wmd=open(md_log,"a")
            wmd.write(b[0].strip("{'log_data': '")+'\n')
            for i in range(1,len(b)-1):
                wmd.write(b[i])
                wmd.write('\n')
            return json.dumps(parse_client_log)
        else:
            return json.dumps(parse_client_log)
    elif action.find("list_cms")!=-1:
        stdata=urldata("http://cms."+urlencode[13:])#stmapi--mapi
        return json.dumps(stdata)
    elif action.find("cart")!=-1:
        stdata=urldata("http://cart."+urlencode[14:])#stmapi--mapi
        return json.dumps(stdata)
    elif action.find("list_category")!=-1:
        stdata=urldata("http://search."+urlencode[16:])#stmapi--mapi
        return json.dumps(stdata)
    elif action.find("get_product")!=-1:
        stdata=urldata("http://product."+urlencode[17:])#stmapi--mapi
        return json.dumps(stdata)
    else:
        stdata=urldata("http://"+urlencode[9:])#stmapi--mapi
        return json.dumps(stdata)
if __name__ == '__main__':
    app.debug = True
    app.run(host='127.0.0.1',port=80)